package com.aaa.gateway;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
public class UIDCheckGateWayFilter implements GatewayFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String url=exchange.getRequest().getPath().pathWithinApplication().value();
        log.info("请求URL:"+url);
        log.info("method:"+exchange.getRequest().getMethod());
        String uId=exchange.getRequest().getQueryParams().getFirst("uid");
        log.info("uid:"+uId);
        if (StringUtils.length(uId) < 3) {
            log.info("参数验证不通过，长度为3~8位");
            //终止请求，直接回应
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
//值越小优先级越高

    @Override
    public int getOrder() {
        return 0;
    }
}
